#include "VistaQt.h"


VistaQt::VistaQt(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::VistaQt)
{
    ui->setupUi(this);
    su_modelo = new tablero();
    Canvas = ui->graphicsView;
    Escena = new QGraphicsScene(0,0,this->width(),this->height());
    Escena->setSortCacheEnabled(true);
    Variante = ui->Variantes;
    Dimension = ui->spinBox;
    Canvas->setScene(Escena);
    timer = new QTimer(this);
    X=-1;
    Y=-1;

    QObject::connect(Variante,SIGNAL(activated(int)), this, SLOT(on_Variantes_activated(int)));
    QObject::connect(Dimension,SIGNAL(valueChanged(int)), this , SLOT(on_spinBox_valueChanged(int)));
    QObject::connect(ui->actionSalir, SIGNAL(triggered()), this , SLOT(on_actionSalir_triggered()));
    QObject::connect(timer,SIGNAL(timeout()), this, SLOT(on_timeout()));
    QObject::connect(ui->ButtonStart,SIGNAL(clicked()), this, SLOT(on_ButtonStart_clicked()));
    QObject::connect(ui->ButtonStop,SIGNAL(clicked()),this,SLOT(on_ButtonStop_clicked()));
    QObject::connect(Canvas, SIGNAL(mousePress(int,int)), this, SLOT(on_mousePress(int,int)));
    QObject::connect(Canvas,SIGNAL(mouseMove(int,int)), this, SLOT(on_mouseMove(int,int)));
    QObject::connect(Canvas, SIGNAL(mouseRelease(int,int)), this , SLOT(on_mouseRelease(int,int)));
    QObject::connect(this, SIGNAL(iconSizeChanged(QSize)), this, SLOT(on_iconSizeChanged(QSize)));
    QObject::connect(ui->actionAbrir,SIGNAL(triggered()),this, SLOT(on_actionAbrir_triggered()));
    QObject::connect(ui->actionNuevo, SIGNAL(triggered()),SLOT(on_actionNuevo_triggered()));
    QObject::connect(ui->actionAcercaDe, SIGNAL(triggered()),SLOT(on_AcercaDe_Triggerred()));


    on_spinBox_valueChanged(16);
}

VistaQt::~VistaQt()
{
    delete ui;
    delete timer;
    delete su_modelo;
    delete Escena;
}


void VistaQt::actualizar()
{
}

void VistaQt::on_actionSalir_triggered()
{
    qApp->quit();
}

void VistaQt::on_Variantes_activated(int index)
{
    ((tablero*)su_modelo)->cambiaVariante(index);
}

void VistaQt::on_spinBox_valueChanged(int arg1)
{
    delete su_modelo;
    su_modelo = new tablero(arg1,Variante->currentIndex(),true);
    sizeW=Escena->width()/arg1;
    sizeH=Escena->height()/arg1;

    recorteX = Escena->width() - (sizeW*arg1);
    recorteY = Escena->height() - (sizeH*arg1);

    recorteX/=2;
    recorteY/=2;

    Pinta2();

}

void VistaQt::Pinta2()
{
    int arg1 = Dimension->value();
    Escena->clear();
    for (int var = 0; var <= arg1; ++var) {
        Escena->addLine(recorteX+var*sizeW, recorteY ,recorteX+var*sizeW, recorteY+arg1*sizeH);
        Escena->addLine(recorteX, recorteY+var*sizeH, recorteX+arg1*sizeW, recorteY+var*sizeH);
    }
for (int j = 0; j < arg1; j++) {
    for (int i = 0; i < arg1; i++) {

            if(!((tablero*)su_modelo)->getEstado(i,j))
            {
                //Escena->addRect(recorteX+i*sizeW+2, recorteY+j*sizeH+2, sizeW-3, sizeH-3, QPen("Black"), QBrush(QColor("White"),Qt::NoBrush));
            }
            else
            {
                Escena->addRect(recorteX+i*sizeW+2, recorteY+j*sizeH+2, sizeW-3, sizeH-3, QPen("Green"), QBrush(QColor("Red"),Qt::SolidPattern)); //recorteX+((i+1)*sizeW)-3, recorteY+((j+1)*sizeH)-3
            }
        }
    }
}

void VistaQt::Pinta(int tempposx, int tempposy)
{
    if(!((tablero*)su_modelo)->getEstado(tempposx,tempposy))
    {
        QGraphicsItem* item=Escena->itemAt(recorteX+tempposx*sizeW+2, recorteY+tempposy*sizeH+2);
        if(item != NULL)
        Escena->removeItem(item);
    }
    else
    {
        Escena->addRect(recorteX+tempposx*sizeW+2, recorteY+tempposy*sizeH+2, sizeW-3, sizeH-3, QPen("Green"), QBrush(QColor("Red"),Qt::SolidPattern)); //recorteX+((i+1)*sizeW)-3, recorteY+((j+1)*sizeH)-3
    }
}

void VistaQt::Pinta3()
{
    int arg1 = Dimension->value();
for (int j = 0; j < arg1; j++) {
    for (int i = 0; i < arg1; i++) {

            QGraphicsItem* item=Escena->itemAt(recorteX+i*sizeW+2, recorteY+j*sizeH+2);
            if(!((tablero*)su_modelo)->getEstado(i,j))
            {
                if(item != NULL)
                {
                    Escena->removeItem(item);
                }
            }
            else
            {
                if(item == NULL)
                {
                    Escena->addRect(recorteX+i*sizeW+2, recorteY+j*sizeH+2, sizeW-3, sizeH-3, QPen("Green"), QBrush(QColor("Red"),Qt::SolidPattern)); //recorteX+((i+1)*sizeW)-3, recorteY+((j+1)*sizeH)-3
                }
            }
            qApp->processEvents();
        }
    }
}

void VistaQt::on_ButtonStart_clicked()
{
    timer->start(1000);
}

void VistaQt::on_ButtonStop_clicked()
{
    timer->stop();
}

void VistaQt::on_timeout()
{
    ((tablero*)su_modelo)->calculaSiguienteEtapa();
    Pinta3();
}

void VistaQt::on_mousePress(int tempposx, int tempposy)
{
    ActualizaEstado(tempposx,tempposy);
}

void VistaQt::on_mouseMove(int movex , int movey)
{
    int tempposx = movex;
    int tempposy = movey;

    tempposx -= recorteX;
    tempposy -= recorteY;

    tempposx /= sizeW;
    tempposy /= sizeH;


    int tempposx2 = X;
    int tempposy2 = Y;

    tempposx2 -= recorteX;
    tempposy2 -= recorteY;

    tempposx2 /= sizeW;
    tempposy2 /= sizeH;

    if(tempposx != tempposx2 || tempposy != tempposy2)
    {
        ActualizaEstado(movex,movey);
    }
}

void VistaQt::on_mouseRelease(int tempposx, int tempposy)
{
    X = -1;
    Y = -1;
}


void VistaQt::ActualizaEstado(int tempposx, int tempposy)
{
    X = tempposx;
    Y = tempposy;

    tempposx -= recorteX;
    tempposy -= recorteY;

    tempposx /= sizeW;
    tempposy /= sizeH;

    if(tempposx < Dimension->value() && tempposy < Dimension->value() && tempposx>=0 && tempposy>=0)
    {
        ((tablero*)su_modelo)->cambiaEstado(tempposx , tempposy);
        Pinta(tempposx, tempposy);
    }
}

void VistaQt::on_iconSizeChanged(QSize iconSize)
{
     Escena = new QGraphicsScene(0,0,this->width(),this->height());
     Canvas->setScene(Escena);
}


void VistaQt::on_actionAbrir_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"",tr("Files (*.*)"));

    tablero * temp = new tablero(fileName.toStdString(), Variante->currentIndex());
    if(((tablero*)temp)->getRows() != 0)
    {
        delete su_modelo;
        su_modelo = temp;

        int arg1 = ((tablero*)su_modelo)->getRows();
        ui->spinBox->setValue(arg1);
        sizeW=Escena->width()/arg1;
        sizeH=Escena->height()/arg1;

        recorteX = Escena->width() - (sizeW*arg1);
        recorteY = Escena->height() - (sizeH*arg1);

        recorteX/=2;
        recorteY/=2;

        Pinta2();
    }

}

void VistaQt::on_actionGuardar_triggered()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Open File"),"",tr("Files (*.*)"));
    ((tablero*)su_modelo)->guarda(fileName.toStdString());
}

void VistaQt::on_actionNuevo_triggered()
{
    int arg1 = ui->spinBox->value();
    delete su_modelo;
    su_modelo = new tablero(arg1,Variante->currentIndex(),false);
    sizeW=Escena->width()/arg1;
    sizeH=Escena->height()/arg1;

    recorteX = Escena->width() - (sizeW*arg1);
    recorteY = Escena->height() - (sizeH*arg1);

    recorteX/=2;
    recorteY/=2;

    Pinta2();
}

void VistaQt::on_AcercaDe_Triggerred()
{
    QMessageBox::about(this, "JuegoVida", "AlejandroSerrano\nPEI-2012\nJuegoVida\n");

}
